package cn.iocoder.yudao.module.dbit.controller.admin.policynew;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.policynew.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.policynew.PolicyNewDO;
import cn.iocoder.yudao.module.dbit.service.policynew.PolicyNewService;

@Tag(name = "管理后台 - 政策新闻")
@RestController
@RequestMapping("/dbit/policy-new")
@Validated
public class PolicyNewController {

    @Resource
    private PolicyNewService policyNewService;

    @PostMapping("/create")
    @Operation(summary = "创建政策新闻")
    @PreAuthorize("@ss.hasPermission('dbit:policy-new:create')")
    public CommonResult<Integer> createPolicyNew(@Valid @RequestBody PolicyNewSaveReqVO createReqVO) {
        return success(policyNewService.createPolicyNew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新政策新闻")
    @PreAuthorize("@ss.hasPermission('dbit:policy-new:update')")
    public CommonResult<Boolean> updatePolicyNew(@Valid @RequestBody PolicyNewSaveReqVO updateReqVO) {
        policyNewService.updatePolicyNew(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除政策新闻")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:policy-new:delete')")
    public CommonResult<Boolean> deletePolicyNew(@RequestParam("id") Integer id) {
        policyNewService.deletePolicyNew(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得政策新闻")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PolicyNewRespVO> getPolicyNew(@RequestParam("id") Integer id) {
        PolicyNewDO policyNew = policyNewService.getPolicyNew(id);
        return success(BeanUtils.toBean(policyNew, PolicyNewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得政策新闻分页")
    public CommonResult<PageResult<PolicyNewRespVO>> getPolicyNewPage(@Valid PolicyNewPageReqVO pageReqVO) {
        PageResult<PolicyNewDO> pageResult = policyNewService.getPolicyNewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PolicyNewRespVO.class));
    }

    @GetMapping("/latest")
    @Operation(summary = "学生端获取n条最新的政策新闻")
    public CommonResult<List<StudentPolicyNewReqVO>> getLatestPolicyNew(@RequestParam("number") Integer number) {
        List<StudentPolicyNewReqVO> result = policyNewService.getLatestPolicyNew(number);
        return success(result);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出政策新闻 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:policy-new:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPolicyNewExcel(@Valid PolicyNewPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PolicyNewDO> list = policyNewService.getPolicyNewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "政策新闻.xls", "数据", PolicyNewRespVO.class,
                BeanUtils.toBean(list, PolicyNewRespVO.class));
    }

}